from flask import jsonify, request
from flask.views import MethodView
from app.models.report import Report
# from ..auth import auth_required
from webargs import fields
from webargs.flaskparser import parser


parser_args = {
    'page': fields.Int(missing=0, error_messages={'invalid': '必须是数值类型'})
}


class ReportAPI(MethodView):
    def get(self, id):
        model = Report.query.get_or_404(id)
        return jsonify(success=True, data=model.to_dict())


class ReportsAPI(MethodView):
    def get(self):
        """获取专报
        """
        args = parser.parse(parser_args, request)
        query = Report.query.order_by(Report.id.desc())
        data = Report.to_collection_dict(query, args['page'], 20)
        return jsonify(success=True, data=data)
